package gwtadmin.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.DOMException;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MyWebAdmin implements EntryPoint {

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * Creates panel which informs user that this feature still not implemented.
	 * 
	 * @return Returns VerticalPanel.
	 */
	private VerticalPanel createDontRealized() {
		VerticalPanel vp = new VerticalPanel();
		vp.setSize("200px", "200px");
		vp.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		vp.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
		vp.add(new Label("Sorry, don't realized yet."));
		vp.addStyleName("decorFrame");

		return vp;
	}

	private VerticalPanel createHomePage() {
		VerticalPanel vp = new VerticalPanel();
		vp.addStyleName("decorFrame");
		vp.setSize("200px", "200px");
		vp.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		vp.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
		vp.add(new Label("Hello, this is Swordfish Registry Admin Tool"));

		return vp;
	}

	private DockPanel createHeader() {

		DockPanel dp = new DockPanel();
		dp.setWidth("100%");

		dp.add(new HTML("<img src=\"rsrc/sf_logo.png\">"), DockPanel.WEST);

		HTML grr = new HTML("<h1>Swordfish Registry</h1>");

		dp.setHorizontalAlignment(DockPanel.ALIGN_RIGHT);
		dp.add(grr, DockPanel.EAST);
		dp.setCellVerticalAlignment(grr, DockPanel.ALIGN_MIDDLE);

		return dp;
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		addressBar = new AddressBar(wsdlMan);
		leftMenu = new LeftMenu(wsdlMan);

		vpHome = createHomePage();
		vpDontRealized = createDontRealized();
		
		VerticalPanel mainPanel = new VerticalPanel();
		
		VerticalPanel vp = new VerticalPanel();
		vp.add(addressBar);
		vp.add(mainPanel);
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(leftMenu);
		hp.add(vp);
		
		panelSwitcher = new PanelSwitcher(mainPanel);
		
		RootPanel.get("headerP").add(createHeader());
		RootPanel.get("centralP").add(hp);

		formViewEdit = new FormViewEdit(wsdlMan);
		formUpload = new FormUpload(wsdlMan);
		formSearch = new FormSearch(wsdlMan);
		formWsdlTree = new FormWsdlTree();
		formWsdlList = new FormWsdlList(wsdlMan);

		panelSwitcher.addWidget(vpHome);
		panelSwitcher.addWidget(vpDontRealized);
		panelSwitcher.addWidget(formViewEdit);
		panelSwitcher.addWidget(formUpload);
		panelSwitcher.addWidget(formWsdlList);
		panelSwitcher.addWidget(formSearch);
		panelSwitcher.addWidget(formWsdlTree);
		
		wsdlMan.goHome();
	}

	private class WsdlManager implements IWsdlManager {

		private AsyncCallback<Boolean> acRemoveWsdl = new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				goListWsdl();
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("RPC Error");
			}
		};

		private AsyncCallback<Boolean> acSaveWsdl = new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("RPC Error");
			}
		};
		
		////////////////////////////////////////////////////////////////////////////////////

		@Override
		public String[] parseWsdlList(String listWsdl) {
			try {
				Document messageDom = (Document) XMLParser.parse(listWsdl);

				Element wsdlList = (Element) messageDom.getElementsByTagName(
						"wsdlList").item(0);
				NodeList nodeList = wsdlList.getElementsByTagName("url");

				int cnt = nodeList.getLength();
				String[] strList = new String[cnt];

				for (int i = 0; i < cnt; i++) {
					String wsdlFileName = nodeList.item(i).getFirstChild()
							.toString().substring(2);
					strList[i] = wsdlFileName;
				}

				return strList;

			} catch (DOMException e) {
				return null;
			}
		}

		////////////////////////////////////////////////////////////////////////////////////

		@Override
		public void saveWsdl(String fileName, String wsdl) {

			if (fileName.equals("") || wsdl.equals(""))
				return;

			greetingService.uploadWsdlFile(fileName, wsdl, acSaveWsdl);
		}

		@Override
		public void getWsdl(String fileName, AsyncCallback<String> callback) {
			formViewEdit.showWait(true);
			greetingService.getWsdlFile(fileName, callback);
		}

		@Override
		public void removeWsdl(String fileName) {
			if (Window.confirm("Are you sure removing this WSDL?")) {
				greetingService.removeWsdlFile(fileName, acRemoveWsdl);
			}
		}

		@Override
		public void downloadWsdl(String fileName) {
			Window.Location.replace("mywebadmin/updo?download=" + fileName);
		}

		@Override
		public void updateWsdlList(AsyncCallback<String> callback) {
			formWsdlList.showWait(true);
			greetingService.getWsdlList(callback);
		}

		@Override
		public void searchWsdl(boolean byPortType, String portType, String namespace, AsyncCallback<String> callback) {
			formSearch.showWait(true);
			greetingService.getWsdlList(byPortType, portType, namespace, callback);
		}

		@Override
		public void getWsdlTree(AsyncCallback<WsdlTreeInfo> callback) {
			formWsdlList.showWait(true);
			greetingService.getWsdlTree(callback);
		}

		@Override
		public void goHome() {
			setAddress("Home");
			panelSwitcher.SwitchTo(vpHome);
		}

		@Override
		public void goBack() {
			panelSwitcher.goBack();
		}
		
		@Override
		public void goEditWsdl(String wsdlName) {
			setAddress("Home > WSDL > " + wsdlName);
			panelSwitcher.SwitchTo(formViewEdit);
			formViewEdit.view(wsdlName);
		}

		@Override
		public void goListWsdl() {
			setAddress("Home > WSDL");
			updateWsdlList(formWsdlList);
			panelSwitcher.SwitchTo(formWsdlList);
		}

		@Override
		public void goListSchema() {
			setAddress("Home > Schema");
			panelSwitcher.SwitchTo(vpDontRealized);
		}

		@Override
		public void goListPolicy() {
			setAddress("Home > Policy");
			panelSwitcher.SwitchTo(vpDontRealized);
		}

		@Override
		public void goWsdlTree() {
			setAddress("Home > WSDL Tree");
			getWsdlTree(formWsdlTree);
			panelSwitcher.SwitchTo(formWsdlTree);
		}

		@Override
		public void goUploadWsdl() {
			setAddress("Home > Upload WSDL");
			panelSwitcher.SwitchTo(formUpload);
		}

		@Override
		public void goUploadSchema() {
			setAddress("Home > Upload Schema");
			panelSwitcher.SwitchTo(vpDontRealized);
		}

		@Override
		public void goUploadPolicy() {
			setAddress("Home > Upload Policy");
			panelSwitcher.SwitchTo(vpDontRealized);
		}

		@Override
		public void goSearch() {
			setAddress("Home > Search");
			panelSwitcher.SwitchTo(formSearch);
		}

		@Override
		public void setAddress(String addr) {
			addressBar.setAddress(addr);
		}
	}

	private LeftMenu 	leftMenu;
	private AddressBar 	addressBar;

	private FormUpload 		formUpload;
	private FormViewEdit 	formViewEdit;
	private FormWsdlList 	formWsdlList;
	private FormSearch 		formSearch;
	private FormWsdlTree 	formWsdlTree;

	private VerticalPanel 	vpHome;
	private VerticalPanel 	vpDontRealized;

	private WsdlManager 	wsdlMan = new WsdlManager();
	private PanelSwitcher 	panelSwitcher;
}
